package com.example.jl.bethinner.activity;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;
import com.example.jl.bethinner.base.BtApplication;
import com.example.jl.bethinner.view.ToggleButton;

import wq.bethinner.zxinglibrary.activity.CaptureActivity;
import wq.bethinner.zxinglibrary.activity.QRCodeWriterActivity;

public class TestListMainActivity extends BaseActivity implements View.OnClickListener {

    private ToggleButton toggleButton;
    private static int SETGPS = 100;
    private static int QRSCCN = 101;
    private LocationClient mLocationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_list_main);
        toggleButton = (ToggleButton) findViewById(R.id.test_toggle_button);
        findViewByIdAndSetOnclickListener(R.id.rxjava_activity);
        findViewByIdAndSetOnclickListener(R.id.ibeacon_activity);
        findViewByIdAndSetOnclickListener(R.id.location_activity);
        findViewByIdAndSetOnclickListener(R.id.get_days_before_now);
        findViewByIdAndSetOnclickListener(R.id.zXing);
        findViewByIdAndSetOnclickListener(R.id.qcode_writer);
        findViewByIdAndSetOnclickListener(R.id.auto_complete_text);
        findViewByIdAndSetOnclickListener(R.id.hello_chart);
        findViewByIdAndSetOnclickListener(R.id.test_wx);
        findViewByIdAndSetOnclickListener(R.id.test_include_number);
        findViewByIdAndSetOnclickListener(R.id.test_include_appointed_string);
        findViewByIdAndSetOnclickListener(R.id.test_notification);
        findViewByIdAndSetOnclickListener(R.id.test_valueAnimator);
        findViewByIdAndSetOnclickListener(R.id.test_diyView);
        findViewByIdAndSetOnclickListener(R.id.test_webview_get_image);
        findViewByIdAndSetOnclickListener(R.id.test_picker_UI);
        findViewByIdAndSetOnclickListener(R.id.test_scan_radar);
        findViewByIdAndSetOnclickListener(R.id.test_draggable_gridview);

        setGpsButtonStatus(this);
        //定位
        initLocationAndStart();

        toggleButton.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                startActivityForResult(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"), SETGPS);
            }
        });
    }

    private void setGpsButtonStatus(Context context) {

        if (isOPen(context)) {
            toggleButton.setToggleOn();
        } else {
            toggleButton.setToggleOff();
        }
    }

    private void findViewByIdAndSetOnclickListener(int id) {
        findViewById(id).setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_test_list_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rxjava_activity:
                startActivity(new Intent(this, RxjavaActivity.class));
                break;
            case R.id.ibeacon_activity:
                startActivity(new Intent(this, IBeaconActivity.class));
                break;
            case R.id.location_activity:
                startActivityForResult(new Intent(this, LocationActivity.class), QRSCCN);
                break;
            case R.id.get_days_before_now:
                startActivityForResult(new Intent(this, GetDaysBeforeNowActivity.class), QRSCCN);
                break;
            case R.id.zXing:
                startActivityForResult(new Intent(this, CaptureActivity.class), QRSCCN);
                break;
            case R.id.qcode_writer:
                QRCodeWriterActivity.actionStart(this, "betttt");
                break;
            case R.id.auto_complete_text:
                startActivity(new Intent(this, AutoCompleteTextViewActivity.class));
                break;
            case R.id.hello_chart:
                startActivity(new Intent(this, HelloChartActivity.class));
                break;
            case R.id.test_wx:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.test_include_number:
                startActivity(new Intent(this, TestIncludeNumberActivity.class));
                break;
            case R.id.test_include_appointed_string:
                startActivity(new Intent(this, TestIncludeAppointedStringActivity.class));
                break;

            case R.id.test_notification:
                startActivity(new Intent(this, NotificationActivity.class));
                break;
            case R.id.test_valueAnimator:
                startActivity(new Intent(this, ValueAnimatorActivity.class));
                break;
            case R.id.test_diyView:
                startActivity(new Intent(this, DIYViewActivity.class));
                break;
            case R.id.test_webview_get_image:
                startActivity(new Intent(this, WebViewActivity.class));
                break;
            case R.id.test_picker_UI:
                startActivity(new Intent(this, PickUIActivity.class));
                break;
            case R.id.test_scan_radar:
                startActivity(new Intent(this, ScanRadarActivity.class));
                break;
            case R.id.test_draggable_gridview:
                startActivity(new Intent(this, DraggableGridViewSampleActivity.class));
                break;

        }
    }

    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps) {
            return true;
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SETGPS) {
            setGpsButtonStatus(this);
        } else {
            if (resultCode == RESULT_OK) {
                if (QRSCCN == requestCode) {
                    Toast.makeText(this, data.getStringExtra("result"), Toast.LENGTH_SHORT).show();
                } else {

                }
            }
        }
    }


    private void initLocationAndStart() {
        mLocationClient = ((BtApplication) getApplication()).mLocationClient;
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 10000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认false，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }
}
